//
//  CSGXCheckPreviewCell.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/22.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXCheckPreviewCell.h"
#import "GXBaseTableViewCell.h"
#import "LKGXNumberModificationView.h"
#import "CSGXDisplayView.h"

#import "CSGXCommodityPickerBLL.h"

#import "TInventoryModel.h"

@interface CSGXCheckPreviewCell () <NumberModificationViewDelegate>

@property(nonatomic,weak) UILabel *lblIndex;
@property (nonatomic,weak) UILabel * lblName;
//参考：8箱2瓶
@property (nonatomic, weak) UILabel *lblInventory;
/// 大单位
@property(nonatomic,weak) LKGXNumberModificationView *bigTypeView;
/// 小单位
@property(nonatomic,weak) LKGXNumberModificationView *smallTypeView;

@property (nonatomic, weak) UILabel *lblTimeRemark;



///修改的单位索引
@property(nonatomic, assign) NSInteger currentCountModiTag;

@property (nonatomic, strong) NSArray<LKGXNumberModificationView *> * numModiArr;



@end

@implementation CSGXCheckPreviewCell


#pragma mark - actions
- (void)btnClear_Click:(UIButton *)sender {
    // 清零
    self.bigTypeView.commodityCount = 0;
    self.smallTypeView.commodityCount = 0;
    self.checkPreviewInventoryModel.iInventoryCount = @",";
    [self redistributePreviewCell];
    
#pragma mark call back
    if (self.callback_checkPreviewCell) {
        self.callback_checkPreviewCell();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    //    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




#pragma mark - methods

- (void)redistributePreviewCell {
    for (int i = 0; i < self.numModiArr.count; i++) {
        self.numModiArr[i].commodityCount = 0;
    }
}

- (void)distributePreview {
    
    NSArray * unitArr = [self.checkPreviewInventoryModel.iUnit componentsSeparatedByString:@","];
    
    NSArray<NSString *> * unitCountArr = [self.checkPreviewInventoryModel.iInventoryCount componentsSeparatedByString:@","];
    
    TInventoryModel *originInventoryModel = [CSGXCommodityPickerBLL loadInventoryModelWithCommNo:self.checkPreviewInventoryModel.iCoNo];
    NSArray *originCountArr = [originInventoryModel.iInventoryCount componentsSeparatedByString:@","];
    NSString *originCountStr = @"参考：";
    
    self.lblName.text = self.checkPreviewInventoryModel.iName;
    self.lblIndex.text = [@(self.cellIndex + 1) gx_toString];
    
    originCountStr = [originCountStr stringByAppendingFormat:@"%@%@,", originCountArr[0], unitArr[0]];
    self.bigTypeView.dataNumModiDict = @{@"unitType": unitArr[0], @"unitCount":unitCountArr[0]};
    NSMutableArray<LKGXNumberModificationView *> * tempNumModiArrM = [NSMutableArray<LKGXNumberModificationView *> array];
    for (int i = 1; i < unitArr.count; i++) {
        // 1. 大单位已经创建，再创建其他小单位。
        // 2. 布局
        LKGXNumberModificationView *numModiView = [LKGXNumberModificationView modificationView];
        [self.contentView addSubview:numModiView];
        
        numModiView.countDelegate = self;
        numModiView.dataNumModiDict = @{@"unitType": unitArr[i], @"unitCount":unitCountArr[i]};
        numModiView.tag = i;
        
        [tempNumModiArrM addObject:numModiView];
        
        //originCountStr
        originCountStr = [originCountStr stringByAppendingFormat:@"%@%@,", originCountArr[i], unitArr[i]];
    }
    self.numModiArr = tempNumModiArrM.copy;
    originCountStr = [originCountStr subStringFrontOfLastIndex:1];
    
    // 布局
    for (int i = 0; i < tempNumModiArrM.count; i ++) {
        if (i == 0) {
            [tempNumModiArrM[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.equalTo(self.bigTypeView);
                make.leading.equalTo(self.bigTypeView.mas_trailing).offset(5);
            }];
        }else {
            [tempNumModiArrM[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.equalTo(self.bigTypeView);
                make.leading.equalTo(tempNumModiArrM[i-1].mas_trailing).offset(5);
            }];
        }
    }
    
    //参考：8箱2瓶
    self.lblInventory.text = originCountStr;
    //生产日期
    self.lblTimeRemark.text = [NSString stringWithFormat:@"备注：%@", self.checkPreviewInventoryModel.iCreateTime];
    
    [self modityCellForegroundColorWithInventoryCheckType:(InventoryCheckType)self.checkPreviewInventoryModel.iIsChecked];
}

- (void)modityCellForegroundColorWithInventoryCheckType:(InventoryCheckType)checkType {
    switch (checkType) {
        case InventoryCheckTypeNone:
            [self modifyCellForegroundColor:[UIColor blackColor]];
            break;
        case InventoryCheckTypeGain:
            [self modifyCellForegroundColor:[UIColor greenColor]];
            break;
        case InventoryCheckTypeLoss:
            [self modifyCellForegroundColor:[UIColor redColor]];
            break;
        case InventoryCheckTypeLevel:
            [self modifyCellForegroundColor:[UIColor blueColor]];
            break;
            
        default:
            break;
    }
}

- (void)modifyCellForegroundColor:(UIColor *)color {
    self.lblName.textColor = color;
    self.bigTypeView.unitColor = color;
    for (int i = 0; i < self.numModiArr.count; i++) {
        self.numModiArr[i].unitColor = color;
    }
}




#pragma mark - delegate
- (void)numberModificationView:(LKGXNumberModificationView *)numberModificationView commodityCount:(NSInteger)commodityCount {
    //根据单位unit修改Model对应的数量和价格
    self.currentCountModiTag = numberModificationView.tag; //修改的哪个单位的数量
    
    self.checkPreviewInventoryModel.iInventoryCount = [CSGXCommodityPickerBLL modifyCountWithCountStr:self.checkPreviewInventoryModel.iInventoryCount
                                                                                                index:self.currentCountModiTag
                                                                                                count:commodityCount];
    //判断盘点状态并动态改变cell数据的颜色
    TInventoryModel *originInventoryModel = [CSGXCommodityPickerBLL loadInventoryModelWithCommNo:self.checkPreviewInventoryModel.iCoNo];
    //
    InventoryCheckType checkType = [CSGXCommodityPickerBLL getInventoryTypeWithCountStr:self.checkPreviewInventoryModel.iInventoryCount
                                                               originCountStr:originInventoryModel.iInventoryCount
                                                                       commNo:originInventoryModel.iCoNo];
    self.checkPreviewInventoryModel.iIsChecked = checkType;
    [self modityCellForegroundColorWithInventoryCheckType:checkType];
    
#pragma mark back call
    if (self.callback_checkPreviewCell) {
        self.callback_checkPreviewCell();
    }
}


#pragma mark - setupUI

- (void) setupUI {
    CGFloat fontSizeF = 12;
    CGFloat marginTopF = 3;
    
    //lblIndex
    UILabel * lblIndex = [UILabel gx_labelWithText:@"66" fontSize:15 color:[UIColor orangeColor]];
    [self.contentView addSubview:lblIndex];
    self.lblIndex = lblIndex;
    
    lblIndex.textColor = [UIColor redColor];
    lblIndex.textAlignment = NSTextAlignmentCenter;
    
    [lblIndex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.contentView);
        make.width.offset(20);
    }];
    
    //lblName
    UILabel * lblName = [UILabel gx_labelWithText:@"综合商品" fontSize:fontSizeF+2 color:[UIColor blackColor]];
    [self.contentView addSubview:lblName];
    self.lblName = lblName;
    
    [lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.leading.equalTo(lblIndex.mas_trailing).mas_offset(marginTopF);
    }];
    
    // btnClear
    UIButton * btnClear = [UIButton gx_textButton:@"清零" fontSize:15 normalColor:kColorBar selectedColor:nil];
    [self.contentView addSubview:btnClear];
    
    [btnClear addTarget:self action:@selector(btnClear_Click:) forControlEvents:UIControlEventTouchUpInside];
    [btnClear.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    
    [btnClear mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(0);
        make.trailing.equalTo(self.contentView).offset(-2);
    }];
    
    //参考：8箱2瓶
    UILabel *lblInventory = [UILabel gx_labelWithText:@"" fontSize:12 color:[UIColor grayColor]];
    [self.contentView addSubview:lblInventory];
    self.lblInventory = lblInventory;
    
    [lblInventory mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnClear.mas_bottom).mas_offset(5);
        make.right.equalTo(btnClear).mas_offset(0);
    }];
 
    // bigTypeView //暂时创建一个
    LKGXNumberModificationView * bigTypeView = [LKGXNumberModificationView modificationView];
    [self.contentView addSubview:bigTypeView];
    self.bigTypeView = bigTypeView;
    
    bigTypeView.countDelegate = self;
    bigTypeView.tag = 0;
    
    [bigTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblInventory.mas_bottom).mas_offset(marginTopF);
        make.leading.equalTo(lblIndex.mas_trailing);
        make.size.mas_offset(CGSizeMake(120, 30));
        make.bottom.equalTo(self.contentView).mas_offset(-5);
    }];
    
//    //
//    UILabel *lblTimeRemark = [UILabel gx_labelWithText:@"" fontSize:13 color:[UIColor blackColor]];
//    [self.contentView addSubview:lblTimeRemark];
//    self.lblTimeRemark = lblTimeRemark;
//    
//    [lblTimeRemark mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(bigTypeView.mas_bottom).mas_offset(3);
//        make.left.equalTo(lblName).mas_offset(0);
//        make.bottom.equalTo(self.contentView).mas_offset(-5);
//    }];
    [self layoutIfNeeded];
}

#pragma mark - setter & getter

- (void)setCheckPreviewInventoryModel:(TInventoryModel *)checkPreviewInventoryModel {
    _checkPreviewInventoryModel = checkPreviewInventoryModel;
    [self distributePreview];
}

- (void)setCellIndex:(NSInteger)cellIndex {
    _cellIndex = cellIndex;
    
    self.lblIndex.text = [@(cellIndex + 1) gx_toString];
}












@end
